package com.itheima.service.impl;

import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 体检预约服务
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;



    //根据预约ID查询预约相关信息（体检人姓名、预约日期、套餐名称、预约类型）
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map != null) {
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(orderDate));
        }
        return map;
    }

    //###################################################################################################
    @Override
    public Map<String, Object> getOrderAndVisit(String begin, String end) {

        Map<String, Object> map = new HashMap<>();
        List<String> days = new ArrayList<>();
        List<Integer> orderCount = new ArrayList<>();

        List<Map<String, Object>> orderList = orderDao.getOrderCountAndDaysByCondition(begin, end);//获得预约数和日期
        if (orderList == null || orderList.size() == 0) {
            throw new RuntimeException("此范围无订单");
        }
        for (Map<String, Object> map1 : orderList) {
            Date orderDate = (Date) map1.get("orderDate");//获得预约日期
            String day = null;
            try {
                day = DateUtils.parseDate2String(orderDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            long count = (long) map1.get("count");//获得预约数
            days.add(day);
            orderCount.add((int) count);
        }
        List<Integer> visitCount = orderDao.getVisitCountByCondition(begin, end);//获得到诊数
        map.put("days", days);
        map.put("orderCount", orderCount);
        map.put("visitCount", visitCount);
        return map;
    }

    @Override
    public List<Map> findAllOrderRecord(String phoneNum,String startDate, String endDate) {

        List<Map> allOrderRecord = orderDao.findAllOrderRecord(phoneNum,startDate, endDate);

        return allOrderRecord;
    }

    @Override
    public Map findOrderDetailsById(Integer id) {

        Map orderDetails = orderDao.findOrderDetailsById(id);

        return orderDetails;
    }








    /**
     * 获取预约设置信息(包含会员名字,预约类型,预约状态,套餐详情)
     *
     * @param id
     * @return
     */
    @Override
    public Map findAllById(Integer id) {
        return orderDao.findAllById(id);
    }


    /**
     * 根据orderID查询套餐id
     *
     * @param id
     * @return
     */
    @Override
    public int findSetmealIdByOrderId(int id) {
        Map map = orderDao.findAllById(id);
        Integer setmealId = (Integer) map.get("setmealId");
        return setmealId;
    }

    /**
     * 编辑预约信息
     * @param order
     * @param setmealId
     */
    @Override
    public void edit(Order order, Integer setmealId) {

        memberDao.editNameById(order);
        order.setSetmealId(setmealId);
        orderDao.edit(order);
    }

    /**
     * 删除预约信息
     * @param id
     */
    public void delete(Integer id) {
        orderDao.deleteById(id);
    }

    /**
     * 更改预约状态
     * @param order
     */
    @Override
    public void changeStatus(Order order) {
        orderDao.changeStatus(order);
    }
}
